package com.littleindian.radioskypipeviewer;

import java.nio.ByteBuffer;

import android.util.Log;


public class BufferProcessor {
	private static String TAG = Viewer.TAG;
	private static String TAG1 = "BufferProcessor";
	
	private enum State {
		procInit,
		procFF,
		procPound,
		proc0,
		procTime,
		procTilde,
		procSpace2,
		procTemp,	
	};
	
	private State mState = State.procInit;
	private String mTime;
	
    public void processBuffer(ByteBuffer bb) {
	// FF # 0 <space> <time> ~ <space> <temperature>
    	byte b;
    	while(bb.hasRemaining()) {
    		b = bb.get();
        	switch(mState) {
        	case procInit:    mState=((b==0xFF)?State.procFF:mState);  break;
        	case procFF:      mState=((b=='#')?State.procPound:State.procInit);  break; 
        	case procPound:   mState=((b=='0')?State.proc0:State.procInit);  break; 
        	case proc0:       
        		if(b==' ') {
        			mState = State.procTime;
        			mTime = new String();
        		}
        		else {
        			mState = State.procInit;
        		}
        	case procTime:  
        		if(b=='~') {
        			mState = State.procTilde;
        			Log.d(TAG, mTime);
        		}
        		else {
            		mTime += b;
        		}
        		break; 
        	}   
        	Log.d(TAG, TAG1+"state="+mState + " byte="+b);
    	}

    }

}
